//
//  HWOpenServiceInfoController.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/29.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import ZLPhotoBrowser
import Kingfisher
class HWOpenServiceInfoController: HWBaseViewController {
//    var collectionView:UICollectionView!
//    let collectionProtocol = HWOpenServiceInfoProtocol()
    
    var tableView:UITableView!
    var tableViewProtocol = HWOpenServiceInfoProtocol()
    let httpManager = HWHttpManager()
    let mutDataArr = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
        addRefresh()
        
        addAction()
        
    }

}

extension HWOpenServiceInfoController {
    private func initUI() -> Void {
        
        navigationView.titleLabel.text = "开服信息"

        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
        
        view.bringSubviewToFront(navigationView)
    }
    
    private func initFrame() -> Void {
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
    }
}

extension HWOpenServiceInfoController {
    func addAction() -> Void {
        tableViewProtocol.didClickGame = {
//            self.navigationController?.pushViewController(HWHomeController(), animated: true)
        }
        tableViewProtocol.didSelectImage = {
            index,images in
            var datas: [Any] = []
            for a in images {
                datas.append(URL(string: a)!)
            }
            let vc = ZLImagePreviewController(datas: datas, index: index, showSelectBtn: false) { (url) -> ZLURLType in
                
                return .image
            } urlImageLoader: { (url, imageView, progress, loadFinish) in
                imageView.kf.setImage(with: url) { (receivedSize, totalSize) in
                    let percentage = (CGFloat(receivedSize) / CGFloat(totalSize))
                    debugPrint("\(percentage)")
                    progress(percentage)
                } completionHandler: { (_) in
                    loadFinish()
                }
            }
            
            vc.doneBlock = { (datas) in
                debugPrint(datas)
            }
            
            vc.modalPresentationStyle = .fullScreen
            self.showDetailViewController(vc, sender: nil)
        }
    }
}

extension HWOpenServiceInfoController {
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    func loadData() -> Void {
        httpManager.getOpenServiceInfo { infoModel in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            if infoModel.count > 0 {
                self.mutDataArr.removeAllObjects()
                self.mutDataArr.addObjects(from: infoModel)
            }
            self.tableViewProtocol.models = self.mutDataArr as? [HWOpenServiceModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            self.view.showHUD(title: error)
        }
    }
    @objc func loadMoreData() -> () {
        tableView.mj_footer?.endRefreshing()
        httpManager.getOpenServiceInfoFooter { models in
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.mutDataArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.mutDataArr as? [HWOpenServiceModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
}
